﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kugar.AdminLte.Core.Test.Managers;
using Kugar.Core.Web;
using Kugar.UI.Web.AdminLTE;
using Kugar.UI.Web.AdminLTE.Authentications;
using Kugar.UI.Web.AdminLTE.Helpers;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Kugar.UI.Web.AdminLTE.PageControls;
using Kugar.UI.Web.AdminLTE.RequestValidate;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;

namespace Kugar.AdminLte.Core.Test
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            //AdminLTEUI.LayoutPath.ResourceBaseUrl=""
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => false;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            
            services.AddSingleton<IAdminLoginManager>(new AdminManager());
            services.AddSingleton<IAdminManager>(new AdminManager());


            services.UseAdminLTE(false);

            services.UseAdminLTERequestValidate();

            services.AddAuthentication()
                .AddAdminLTECookie("system", option =>
                {
                    
                    option.LoginManager = new AdminManager();
                    option.MainTitle = "测试管理后台";
                    option.LogoUrl="";
                    option.RegisterUrl = "sdfsfsfs";
                    option.ForgetpasswordUrl = "ssssss";
                    option.loginPageManager=new AdminManager();
                });

            //AdminLTEUI.JqTableEndabled = false;

            services.AddHttpContextAccessor();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);//.AddSessionStateTempDataProvider();

            //services.AddSession(options =>
            //{
            //    // Set a short timeout for easy testing.
            //    options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
            //    options.Cookie.Name = "kugarlte";
            //    options.IdleTimeout = TimeSpan.FromMinutes(15);
            //    options.Cookie.HttpOnly = false;
            //});


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            
            app.UseAdminLTERequestValidateStaticFile();
            
            app.UseAdminLTEStaticFile();

            app.UseAuthentication();
            
            app.UseStaticHttpContext();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "areas",
                    template: "{area:exists}/{controller=Home}/{action=Index}");

                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}");
            });

            
        }


    }

}
